package com.java.simple.study.juc.p4;

import org.openjdk.jcstress.annotations.Actor;
import org.openjdk.jcstress.annotations.Expect;
import org.openjdk.jcstress.annotations.JCStressTest;
import org.openjdk.jcstress.annotations.Outcome;
import org.openjdk.jcstress.annotations.State;
import org.openjdk.jcstress.infra.results.I_Result;

/**
 * @author zhoujunlin
 * @date 2022年06月07日 11:08
 * @desc 执行方式
 * idea中配置 启动类Main Class: org.openjdk.jcstress.Main  固定不变
 * Program arguments: -t VisibilityTest (你的类名) -v
 */
@JCStressTest
@State
@Outcome(id = {"1", "4"}, expect = Expect.ACCEPTABLE, desc = "期待的结果")
@Outcome(id = {"0"}, expect = Expect.ACCEPTABLE_INTERESTING, desc = "令人惊讶的结果")
public class VisibilityTest {

    private int a = 0;
    private volatile boolean flag = false;

    @Actor
    public void method1(I_Result result) {
        if (flag) {
            result.r1 = a * 2;
        } else {
            result.r1 = 1;
        }
    }

    @Actor
    public void method2(I_Result result) {
        // 这个方法里 两个语句可能会发生指令重排 jit
        a = 2;
        flag = true;
    }


}
